layui.use(['table', 'admin', 'ax', 'func','laydate','formSelects', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 评论管理
     */
    var NanSellAnnounceComment = {
        tableId: "nanSellAnnounceCommentTable"
    };


    /**
     * 初始化表格的列
     */
    NanSellAnnounceComment.initColumn = function () {
        return [[
            {align: 'center', toolbar: '#tableBar', width: 160, title: '操作'},
            {field: 'id', sort: false, title: 'ID', width: 100},
            {field: 'content', sort: false, title: '内容'},
            {field: 'userId', sort: false, title: '评论人', width: 100},
            {field: 'replyCount', sort: false, title: '回复数', width: 80},
            {field: 'likeTimes', sort: false, title: '点赞数', width: 80},
            {field: 'createTime', sort: false, title: '评论时间', width: 180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    NanSellAnnounceComment.search = function () {
        var queryData = {};

        queryData['content'] = $('#content').val();

        table.reload(NanSellAnnounceComment.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    NanSellAnnounceComment.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/nanSellAnnounceComment/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    NanSellAnnounceComment.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/nanSellAnnounceComment/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    NanSellAnnounceComment.exportExcel = function () {
        var checkRows = table.checkStatus(NanSellAnnounceComment.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    NanSellAnnounceComment.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/nanSellAnnounceComment/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(NanSellAnnounceComment.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + NanSellAnnounceComment.tableId,
        url: Feng.ctxPath + '/nanSellAnnounceComment/list?announceId=' + Feng.getUrlParam("announceId"),
        page: true,
        limit: 20,
        height: "full-158",
        cellMinWidth: 100,
        cols: NanSellAnnounceComment.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        NanSellAnnounceComment.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    NanSellAnnounceComment.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        NanSellAnnounceComment.exportExcel();
    });

    table.on('sort('+NanSellAnnounceComment.tableId+')', function(obj){
            table.reload(NanSellAnnounceComment.tableId, {
                initSort: obj,
                where: {
                    field: obj.field.replace(/([A-Z])/g,"_$1").toLowerCase()
                    ,order: obj.type
                }
            });
        });

    // 工具条点击事件
    table.on('tool(' + NanSellAnnounceComment.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            NanSellAnnounceComment.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            NanSellAnnounceComment.onDeleteItem(data);
        }else if(layEvent === 'look'){
            admin.open({
                id: 'lookForm',
                type: 2,
                title: "查看回复",
                shade: 0,
                area: ['1000px', '600px'],
                content: '/nanSellAnnounceReply?commentId=' + data.id
            });
        }else if(layEvent === 'reply'){
            admin.open({
                id: 'replyForm',
                type: 2,
                title: "回复评论",
                shade: 0,
                area: ['500px', '300px'],
                content: '/nanSellAnnounceReply/reply?commentId=' + data.id
            });
        }else if (layEvent === 'chat') {
            window.parent.openChat(data.userId);
        }
    });

    window.openChat = window.parent.openChat;
});
